﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Snake.Data;
using Snake.Net;

namespace Snake
{
    public partial class Frm_Game : Form
    {
        string barrier;
        string account;
        int barid;
        int userid;

        public Frm_Game()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            barrier = File.Read("barrier_1.skb");
        }

        public Frm_Game(int id,string username)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            barid = id;
            barrier = HttpRequest.GetBarrierJson(id);
            account = username;
            userid = HttpRequest.GetUserID(account);
        }

        Graphics g;
        Scene scene;
        private void Start_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();

            //barrier = File.Read("barrier.skb");
            scene = BarrierOperater.GetSceneFromJson(barrier, g);
            scene.GameOvered += Scene_GameOvered;
            scene.ScoreRefreshed += Scene_ScoreRefreshed;
            scene.Run();
            btn_Pause.Enabled = true;
            btn_Start.Enabled = false;
        }

        bool pauseFlag = false;

        protected override bool ProcessCmdKey(ref Message msg, Keys KeyData)
        {
            switch (KeyData)
            {
                case Keys.Up:
                    if (scene.GetSnakeDirection() == Snake.SnakeDirection.Up || scene.GetSnakeDirection() == Snake.SnakeDirection.Down) break;
                    scene.ChangeDirection(Snake.SnakeDirection.Up);
                    Scene.Trunflag = 40;
                    break;
                case Keys.Down:
                    if (scene.GetSnakeDirection() == Snake.SnakeDirection.Up || scene.GetSnakeDirection() == Snake.SnakeDirection.Down) break;
                    scene.ChangeDirection(Snake.SnakeDirection.Down);
                    Scene.Trunflag = 40;
                    break;
                case Keys.Left:
                    if (scene.GetSnakeDirection() == Snake.SnakeDirection.Left || scene.GetSnakeDirection() == Snake.SnakeDirection.Right) break;
                    scene.ChangeDirection(Snake.SnakeDirection.Left);
                    Scene.Trunflag = 40;
                    break;
                case Keys.Right:
                    if (scene.GetSnakeDirection() == Snake.SnakeDirection.Left || scene.GetSnakeDirection() == Snake.SnakeDirection.Right) break;
                    scene.ChangeDirection(Snake.SnakeDirection.Right);
                    Scene.Trunflag = 40;
                    break;
                case Keys.P:
                    if (pauseFlag)
                    {
                        btn_Pause.Text = "Continue";
                        scene.Pause();
                    }
                    else
                    {
                        btn_Pause.Text = "Pause";
                        scene.Continue();
                    }
                    pauseFlag = !pauseFlag;
                    break;
                case Keys.S:
                    new Frm_Mode().ShowDialog();
                    break;

            }
            return true;
        }



        private void Scene_ScoreRefreshed(int score)
        {
            lab_Score.Text = score.ToString();
            lbl_len.Text = scene.Snake.Components.Count.ToString();
        }

        private void Scene_GameOvered()
        {
            //MessageBox.Show("游戏结束！\n当前分数为"+lab_Score.Text+"\n分数上传中......");
            var result = HttpRequest.UpLoadRanking(barid, userid, scene.Score, scene.Snake.Components.Count);
            if(result=="OK")
            {
                MessageBox.Show("分数上传成功");
            }
            else
            {
                MessageBox.Show(result);
            }
            btn_Start.Enabled = true;
            btn_Pause.Enabled = false;
        }

        private void Form_Game_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void Btn_Pause_Click(object sender, EventArgs e)
        {
            if (pauseFlag)
            {
                btn_Pause.Text = "Continue";
                scene.Pause();
            }
            else
            {
                btn_Pause.Text = "Pause";
                scene.Continue();
            }
            pauseFlag = !pauseFlag;
        }

        private void Frm_Game_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btn_settings_Click(object sender, EventArgs e)
        {
            new Frm_Mode().ShowDialog();
        }

        private void Frm_Game_Load(object sender, EventArgs e)
        {

        }

        private void btn_return_Click(object sender, EventArgs e)
        {
            Close();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Frm_Game_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            scene.Dispose();
            Close();
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }
        protected override void OnShown(EventArgs e)
        {
            Graphics g = CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.White, 3f), 0, 0, Width - 3, Height - 3);
        }
    }
}
